
import 'dart:async';

import 'package:flutter/services.dart';

import 'oupay_options.dart';
import 'oupay_result.dart';

class FlutterOupay {
  static const MethodChannel _channel = MethodChannel('flutter_oupay');

  static bool  _options_init = false;
  static OupayOptions? _options;

  static void setOupayOptions(final OupayOptions opt){
    _options = opt;
    _options_init = true;
  }

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }



  static Future<dynamic> get isInstallApps async {
    var installApps = await _channel.invokeMethod('checkInstallApps',<String, dynamic>{
      'unpayAppid': _options!.unpayId,
      'alipayAppid': _options!.alipayId,
      'wechatAppid': _options!.wechatId,
      'cmbAppid': _options!.cmbAppId
    });
    return installApps;
  }

  /**
   * 支付
   * */
  static Future<OupayResult> ouPay(Map<String, dynamic> payOrderInfo) async {
    OupayResult oupayRest = new OupayResult();
    if(!_options_init){

      oupayRest.setOupayRest(-1, "未初始化[ setOupayOptions ]", payChannel: '');
      return oupayRest;
    }

    String channelId = payOrderInfo['channelId'];
    dynamic channelData = payOrderInfo['channelData'];

    if(null == channelId || null == channelData){
      final OupayResult oupayRest = new OupayResult();
      oupayRest.setOupayRest(-1, "参数无效", payChannel: '');
      return oupayRest;
    }

    switch (channelId) {
      case "11":  //uppay
        return _unionpay(channelData);
        break;
      case "12":  //alipay
        //return _alipay(channelData);
        break;
      case "13": //wechatpay
        //return _wechatpay(channelData);
        break;
      case "14": //cmbchina
       // return _cmbchinapay(channelData);
        break;
      case "15": //ccbnet 建设银行
        return _ccbnetpay(channelData);
        break;
      default:
        {
          final OupayResult oupayRest = new OupayResult();
          oupayRest.setOupayRest(-1, "不支持的渠道:$channelId", payChannel: '');
          return oupayRest;
        }
        break;
    }
    return oupayRest;
  }

  /**
   * 银联
   */
  static Future<OupayResult> _unionpay(dynamic payInfo) async{
    try{
      var res =  await _channel.invokeMethod('unionPay',<String, dynamic>{
        'payInfo': payInfo,
        'isSandbox': _options!.isSandboxByUn,
        'urlScheme': _options!.unpayScheme
      });

      final OupayResult oupayRest = new OupayResult();
      if( res['pay_result'] == "success" ){
        oupayRest.setOupayRest(0, "支付完成",payChannel:'unionpay',channelData:res );
      }else if( res['pay_result'] == "cancel"){
        oupayRest.setOupayRest(-2, "用户取消",payChannel:'unionpay',channelData:res );
      }else{
        oupayRest.setOupayRest(-1, "支付失败",payChannel:'unionpay',channelData:res );
      }
      return oupayRest;
    } on PlatformException catch (e) {
      print(e);
      final OupayResult oupayRest = new OupayResult();
      oupayRest.setOupayRest(-1, "银联支付异常[$e]", payChannel: '');
      return oupayRest;
    }

  }

  /**
   * 建设银行
   */
  static Future<OupayResult> _ccbnetpay(dynamic payInfo) async{
      try{
        var res =  await _channel.invokeMethod('ccbnetPay',<String, dynamic>{
          'payInfo': payInfo,
          'isSandbox': _options!.isSandboxByCcb,
          'urlScheme': _options!.ccbScheme
        });

        final OupayResult oupayRest = new OupayResult();
        if( res['pay_result'] == "Y" ){
          oupayRest.setOupayRest(0, "支付完成",payChannel:'ccbnetPay',channelData:res );
        }else if( res['pay_result'] == "cancel"){
          oupayRest.setOupayRest(-2, "用户取消",payChannel:'ccbnetPay',channelData:res );
        }else{
          oupayRest.setOupayRest(-1, "支付失败",payChannel:'ccbnetPay',channelData:res );
        }
        return oupayRest;
      } on PlatformException catch (e) {
        print(e);
        final OupayResult oupayRest = new OupayResult();
        oupayRest.setOupayRest(-1, "建支付异常[$e]", payChannel: '');
        return oupayRest;
      }
  }


  /**
   * 支付宝
   */
  // static Future<OupayResult> _alipay(dynamic payInfo) async{
  //   try{
  //     var res =  await _channel.invokeMethod('aliPay',<String, dynamic>{
  //       'payInfo': payInfo,
  //       'isSandbox': _options.isSandboxByAli,
  //       'urlScheme': _options.alipayScheme
  //     });
  //
  //     final OupayResult oupayRest = new OupayResult();
  //     if( res['resultStatus'].toString() == "9000" ){
  //       oupayRest.setOupayRest(0, "支付完成",payChannel:'alipay',channelData:res );
  //     }else if( res['resultStatus'].toString() == "6001"){
  //       oupayRest.setOupayRest(-2, "用户取消",payChannel:'alipay',channelData:res );
  //     }else{
  //       oupayRest.setOupayRest(-1, "支付失败",payChannel:'alipay',channelData:res );
  //     }
  //     return oupayRest;
  //   } on PlatformException catch (e) {
  //     print(e);
  //     final OupayResult oupayRest = new OupayResult();
  //     oupayRest.setOupayRest(-1, "支付宝支付异常[$e]");
  //     return oupayRest;
  //   }
  // }

  /**
   * 微信
   */
  // static Future<OupayResult> _wechatpay(dynamic payInfo) async{
  //   try{
  //     var res =  await _channel.invokeMethod('wechatPay',<String, dynamic>{
  //       'payInfo': payInfo,
  //       'appid': _options.wechatId,
  //       'urlScheme': _options.wechatScheme
  //     });
  //
  //     final OupayResult oupayRest = new OupayResult();
  //     if( res['errCode'].toString() == "0" ){
  //       oupayRest.setOupayRest(0, "支付完成",payChannel:'wechatpay',channelData:res );
  //     }else if( res['errCode'].toString() == "-2"){
  //       oupayRest.setOupayRest(-2, "用户取消",payChannel:'wechatpay',channelData:res );
  //     }else{
  //       oupayRest.setOupayRest(-1, "支付失败",payChannel:'wechatpay',channelData:res );
  //     }
  //     return oupayRest;
  //   } on PlatformException catch (e) {
  //     print(e);
  //     final OupayResult oupayRest = new OupayResult();
  //     oupayRest.setOupayRest(-1, "微信支付异常[$e]");
  //     return oupayRest;
  //   }
  // }

}
